package com.example.italkapp2.controller.activity;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.italkapp2.R;

import com.example.italkapp2.model.Model;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.widget.EaseTitleBar;

public class SettingActivity extends Activity {

    private Button btn_setting_logout;
    private EaseTitleBar titleBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        initView();
        initListener();
    }

    private  void initView(){
        btn_setting_logout = findViewById(R.id.btn_setting_logout);
        titleBar = findViewById(R.id.title_bar);
    }

    private void initListener(){
        titleBar.setLeftLayoutClickListener(v -> SettingActivity.this.finish());

        //退出登录
        btn_setting_logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
                    @Override
                    public void run() {
                        //登录环信服务器退出登录
                        EMClient.getInstance().logout(false, new EMCallBack() {

                            //退出成功
                            @Override
                            public void onSuccess() {
                                //关闭DBHelper
                                Model.getInstance().getDbManager().close();

                                SettingActivity.this.runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(SettingActivity.this,"退出成功",Toast.LENGTH_SHORT).show();
                                        //回到登录页面
                                        Intent intent = new Intent(SettingActivity.this, LoginActivity.class);
                                        startActivity(intent);
                                        SettingActivity.this.finish();
                                    }
                                });
                            }

                            //退出失败
                            @Override
                            public void onError(int code, String error) {
                                SettingActivity.this.runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(SettingActivity.this,"退出失败"+error,Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }

                            //退出中
                            @Override
                            public void onProgress(int progress, String status) {

                            }
                        });
                    }
                });
            }
        });

    }
}